/**
 * 
 */
package com.gosophia.commons.exception;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;

import com.gosophia.commons.AOPAspectOrder;

 

/**
 * 功能：ws异常处理
 * 
 * @author zhangdongsheng
 * @since 2010-10-8 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Aspect
public class WSExceptionAOPHandler implements Ordered {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** webservice 层处理 */

    @Around("execution(* com.gosophia.*.ws.impl.*.*(..))")
    public Object webServiceExceptionInterceptor(ProceedingJoinPoint joinPoint)
            throws Throwable {
        logger.info("The method " + joinPoint.getSignature().getName()
                + "() begins with " + Arrays.toString(joinPoint.getArgs()));
   
        Object result = null;
        try {
              result = joinPoint.proceed();
            logger.info("The method " + joinPoint.getSignature().getName()
                    + "() ends with " + result);
            return result;
        } catch (IllegalArgumentException e) {// 参数异常
            
            StringBuilder sb = new StringBuilder();
            sb.append(joinPoint.getTarget().getClass().getName() + " : "
                    + Arrays.toString(joinPoint.getArgs()) + " in "
                    + joinPoint.getSignature().getName() + "()");
            ExceptionDetail detail = new ExceptionDetail();
            detail.setErrorCode(ErrorCodeList.PARAMENTER_ERROR_CODE);
            logException(joinPoint,e);
            throw new ParameterException(sb.toString(), detail, e);
        } 
        catch (ParameterException pe) {
            logException(joinPoint,pe);
            // wrapException(pe,ErrorCodeList.PARAMENTER_ERROR_CODE);
            throw pe;
        }
        catch (ConcurrencyException ce) {
            logException(joinPoint,ce);
           // wrapException(ce,ErrorCodeList.CONCURRENCY_ERROR_CODE);
            throw ce;
        } catch (TechnicalException wse) {
            logException(joinPoint,wse);
            //wrapException(wse,ErrorCodeList.TECHNICAL_ERROR_CODE);
            throw wse;
        }
        //return result;
       

    } 

  
    private void logException(ProceedingJoinPoint joinPoint,Exception e){
        logger.error("Target Class : "
                + joinPoint.getTarget().getClass().getName());
        logger.error("Illegal argument "
                + Arrays.toString(joinPoint.getArgs()) + " in "
                + joinPoint.getSignature().getName() + "()");
    }


	@Override
    public int getOrder() {
	    return AOPAspectOrder.WS_EXCEPTION_ASPECT;
    }    
 
}
